/**
 * 培训需求列表
 */
<template>
	<vp-main-page @refresh="loadDataList" :tableRef="tableRef">
		<template #search>
			<el-form ref="searchFormRef" :model="searchForm" @submit.prevent @keyup.enter="loadDataList" label-width="120px">
				<vp-search-row :show-more="showMore">
					<vp-search-col>
						<el-form-item prop="bh">
							<template #label><vp-label zh="记录单号" en="No. " /></template>
							<el-input v-model="searchForm.bh" v-trim></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="kcbh">
							<template #label><vp-label zh="课程编号" en="Course No."/></template>
							<el-input v-model="searchForm.kcbh" v-trim placeholder="" clearable></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="kcmc">
							<template #label><vp-label zh="课程名称" en="Course Name"/></template>
							<el-input v-model="searchForm.kcmc" v-trim placeholder="" clearable></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="ztList">
							<template #label><vp-label zh="状态" en="Status"/></template>
							<VpStatusMultSelect v-model="searchForm.ztList" />
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="xkkrq">
							<template #label><vp-label zh="需开课日期" en="Required Dete"/></template>
							<el-date-picker
								v-model="searchForm.xkkrq"
								type="daterange"
								start-placeholder="开始日期/Start Date"
								end-placeholder="结束日期/End Date"
								value-format="YYYY-MM-DD"
								unlink-panels
								range-separator="-"
								class="w100"
							>
							</el-date-picker>
						</el-form-item>
					</vp-search-col>
					<vp-search-col btn-col>
						<vp-btn-search :loading="loading" @click="loadDataList"/>
						<vp-btn-reset @click="handleSearchReset"/>
						<vp-btn-more @change="showMore = $event"/>
					</vp-search-col>
				</vp-search-row>
			</el-form>
		</template>
		<template #actions>
			<vp-btn-add v-auth="'trainingNeeds.edit'" @click="handleAdd"/>
		</template>
		<template #table>
			<vp-vxe-table
				id="trainingNeedsTable"
				ref="tableRef" 
				:data="tableData" 
				@sort-change="handleSortChange" 
				row-key="id"
				v-loading="loading"
			>	
				<vxe-column type="seq" align="center" title="#" width="50" />
				<vxe-column 
					sortable
					field="zt" 
					show-overflow
					width="140"
				>
					<template #header><vp-label zh="状态" en="Status"/></template>
					<template #default="scope">
						<span>{{ getStatusDesc(scope.row.zt) }}</span>
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="bh" 
					show-overflow
					width="140"
				>
					<template #header><vp-label zh="编号" en="No."/></template>
					<template #default="scope">
						<span v-html="markTextColor(searchForm.bh, scope.row.bh)"/>
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="kcbh" 
					show-overflow
					width="140"
				>
					<template #header><vp-label zh="课程编号" en="Course No."/></template>
					<template #default="scope">
						<el-link @click="handleCourseView(scope.row.kcid)">R{{ scope.row.kcbh }}</el-link>
					</template>
					
				</vxe-column>
				<vxe-column 
					field="kcmc" 
					show-overflow 
					min-width="150"
				>
					<template #header><vp-label zh="课程名称" en="Course Name" /></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="xkkrq" 
					show-overflow 
					width="180"
				>
					<template #header><vp-label zh="需开课时间" en="Required Time" /></template>
					<template #default="scope">
						{{ (scope.row.xkkrq || '') + ' ' + (scope.row.xkksj || '') }}
					</template>
				</vxe-column>
				<vxe-column 
					field="fsbs" 
					show-overflow 
					width="120"
				>
					<template #header><vp-label zh="初复训标识" en="Retraining"/></template>
					<template #default="scope">
						{{ scope.row.fsbs == 10 ? "初训/Initial" : scope.row.fsbs == 20 ? "复训/Retraining" : "" }}
					</template>
				</vxe-column>
				<vxe-column 
					field="sqyy" 
					show-overflow 
					min-width="120"
				>
					<template #header><vp-label zh="申请原因" en="Reason"/></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="cjsj" 
					show-overflow 
					width="160"
				>
					<template #header><vp-label zh="制单时间" en="Crt. Time"/></template>
				</vxe-column>
				<vxe-column 
					field="operation" 
					title="操作 Operation"
					fixed="right" 
					width="180"
				>
					<template #header><vp-label-operations/></template>
					<template #default="scope">
						<el-space>
							<vp-btn-view text @click="handleView(scope.row.id)"/>
							<vp-btn-edit v-if="scope.row.zt === 10" text v-auth="'trainingNeeds.edit'" @click="handleEdit(scope.row)"/>
							<vp-btn-del v-if="scope.row.zt === 10" text v-auth="'trainingNeeds.edit'" @click="handleDelete(scope.row)"/>
						</el-space>
					</template>
				</vxe-column>
			</vp-vxe-table>
		</template>
		<template #pagination>
			<vp-pagination
				@change="loadDataList"
				:total="page.total" 
				v-model:currentPage="page.pageNum"
				v-model:pageSize="page.pageSize"
			>
			</vp-pagination>
		</template>
		<EditTrainingNeeds ref="editTrainingNeedsRef" @formSubmited="handleFormSubmited"></EditTrainingNeeds>
	</vp-main-page>
</template>
<script setup lang="ts" name="trainingNeeds">
import { onMounted, reactive, ref, defineAsyncComponent } from 'vue'
import request from '/@/utils/request';
import { useRouter } from 'vue-router';
import { DEFAULT_PAGE } from '/@/utils/page';
import VpMessageBox from '/@/components/vp/VpMessageBox';
import VpStatusMultSelect from "/@/components/vp/status/VpStatusMultSelect.vue";
import { markTextColor } from '/@/utils/toolsValidate';
import commonFunction from '/@/utils/commonFunction';

const EditTrainingNeeds = defineAsyncComponent(() => import('./component/EditTrainingNeeds.vue'));

const searchForm = reactive({
	bh: "",
	kcbh: "",
	kcmc: "",
	ztList: [10, 88, 89, 98],
	xkkrq: [],
});

const page = reactive({
	...DEFAULT_PAGE,
})

const showMore = ref(false);
const loading = ref(false);
const tableData = ref([]);

const router = useRouter();

const {getStatusDesc} = commonFunction();

// 加载列表数据
const loadDataList = () => {
	loading.value = true;
	request.get('/training/needs/list', {
		params: {
			...searchForm,
			...page,
		}
	}).then(res => {
		tableData.value = res?.data?.list || [];
		page.total = res?.data?.total || 0;
		page.pageNum = res?.data?.pageNum || 1;
	}).finally(()=>{
		loading.value = false;
	})
}

const searchFormRef = ref();
// 重置搜索条件
const handleSearchReset = ()=>{
	searchFormRef.value.resetFields();
	loadDataList();
}

// 列表字段排序
const handleSortChange = (column: any)=>{
	page.sortColumn = column.field;
	page.sortOrder = column.order;
	loadDataList()
}

onMounted(() => {
	loadDataList();
})

const editTrainingNeedsRef = ref();
const tableRef = ref();

const handleAdd = ()=>{
	editTrainingNeedsRef.value.handleOpen({
		type: 'add',
	});
}

const handleEdit = (row: any)=>{
	editTrainingNeedsRef.value.handleOpen({
		type: 'edit',
		id: row.id
	});
}

const handleFormSubmited = ()=>{
	loadDataList();
}

const handleView = (id: string)=>{
	router.push({
		name: "viewTrainingNeeds",
		params: {
			id: id,
		},
	})
}

const handleCourseView = (id: string)=>{
	router.push({
		name: "viewTrainingCourse",
		params: {
			id: id,
		},
	})
}

// 删除
const handleDelete = (row: any)=>{
	VpMessageBox.confirmDelete().then(()=>{
		loading.value = true;
		request.delete(`/training/needs/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

// 启用
const handleEnable = (row: any)=>{
	VpMessageBox.confirmOperation().then(()=>{
		loading.value = true;
		request.post(`/training/needs/enable/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

// 禁用
const handleDisable = (row: any)=>{
	VpMessageBox.confirmOperation().then(()=>{
		loading.value = true;
		request.post(`/training/needs/disable/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

</script>

<style scoped lang="scss">
</style>